<?php
class access extends base
{

	public function init(){
		parent::init();

		$this->tit = '权限';
		$this->modelObj = D('sys_access');

		$this->type = $this->tab + 1;
		$this->data_list = $this->modelObj->get_treeList("status=1 and type={$this->type}",'id asc');
	}

    //列表
	public function index()
	{
		$this->tabtit = array_merge($this->typeRadio,array('禁用'));
		$cond = "1=1";
		//加入查询条件
		switch($this->tab){
			case '1':
				$cond .=" and type=2 and status=1";
				break;
			case '2':
				$cond .=" and type=3 and status=1";
				break;
			case '3':
				$cond .=" and status=0";
				break;
			default:
				$cond .=" and type=1 and status=1";
		}
		$this->list = $this->modelObj->get_treeList($cond,'sort asc, id asc');
		if($this->tab == 3){
			$this->list = $this->modelObj->get_list($cond);
		}
	}

	//添加
	public function add()
	{
		if($this->isAjax()){
			$data = $this->postData('data');
			$res = $this->modelObj->add($data);
			$this->renderSuccess('添加成功');
		}
		$this->info = array('status'=>1,'sort'=>100,'type'=>$this->type);
		$this->display($this->routeUrl."/set.php");
	}

	//编辑
	public function edit(){
		$id = arg('id');
		if($this->isAjax()){
			$data = $this->postData('data');
			$this->modelObj->edit($id,$data);
			$this->renderSuccess('编辑成功', "/{$this->routeUrl}/index");
		}
		$this->info = $this->modelObj->findById($id);
		$this->display($this->routeUrl."/set.php");
	}

	//显示
	public function act()
	{
		$id = arg('id');
		$status = arg('status');
		$msg = ($status==1)?'显示':'隐藏';
		$opid = arg('opid');
		if($this->isAjax()){
			if($id){
				$res = $this->modelObj->act($id,$status);
				$this->renderSuccess($msg.'成功');
			}else{
				if($opid){
					foreach($opid as $val){
						$res = $this->modelObj->act($val,1);
					}
					$this->renderSuccess('显示成功');
				}else{
					$this->renderError('请选择数据');
				}
			}
		}
	}

	//隐藏
	public function ban()
	{
		$opid = arg('opid');
		if($this->isAjax()){
			if($opid){
				foreach($opid as $val){
					$res = $this->modelObj->act($val,0);
				}
				$this->renderSuccess('隐藏成功');
			}
			$this->renderError('请选择数据');
		}
	}

    //删除
	public function del()
	{
		$id = arg('id');
		$opid = arg('opid');
		if($this->isAjax()){
			if($id){
				$res = $this->modelObj->del($id);
				$this->renderSuccess('删除成功');
			}else{
				if($opid){
					foreach($opid as $val){
						$res = $this->modelObj->del($val);
					}
					$this->renderSuccess('删除成功');
				}else{
					$this->renderError('请选择数据');
				}
			}

		}
	}



	//排序
	public function sort()
	{
        $sort = arg('sort');
		$result = $this->modelObj->sort($sort);
		$this->renderSuccess('排序成功');
	}


	//生成
	public function make()
	{
		$id = arg('id');
		if($this->isAjax()){
			$info = $this->modelObj->get_info($id);
			$result = $this->modelObj->make_add($info);
			$this->renderSuccess('生成PHP成功');
		}
	}

}